/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-09-03 01:51:43
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-30 15:59:47
 * @FilePath: \keep_motion_api\controllers\course\courses.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-09-03 01:51:43
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-19 14:57:30
 * @FilePath: \keep_motion_api\controllers\course\courses.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Course } = require('@/models/course');
const { clearObject } = require('@/utils');

const getCourses = async (req, res) => {
  try {
    const {_id, title, category, difficulty, status, pageNum = 1, pageSize = 20 } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Course.getCourses(_id, title, category, difficulty, status, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询课程列表失败',error });
  }
}
const getCourseDetail = async (req, res) => {
  try {
    const { id } = req.params;
    const course = await Course.getCourseDetail(id);
    res.status(200).send(course);
  } catch (error) {
    res.status(500).json({ message: '查询课程详情失败', error });
  }
}
const createCourses = async (req, res) => {
  try {
    const instructor = req.user.userId;
    const body = req.body;
    const params = clearObject(body);
    const course = await Course.createCourses({...params, instructor});
    res.status(200).send(course);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const updateCourses = async (req, res) => {
  try {
    const { id } = req.params;
    const body = req.body;
    const params = clearObject(body);
    const course = await Course.updateCourses(id, params);
    res.status(200).send(course);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const deleteCourses = async (req, res) => {
  try {
    const { id } = req.params;
    const course = await Course.deleteCourses(id);
    res.status(200).send(course);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const publishCourses = async (req, res) => {
  try {
    const { id } = req.params;
    const course = await Course.publishCourses(id);
    res.status(200).send(course);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}

const searchCourses = async (req, res) => {
  const { user_id, course_id } = req.params;
  const course = await Course.searchCourses(user_id, course_id);
  res.json(course);
}
const getCoursesStats = async (req, res) => {
  const { user_id, course_id } = req.params;
  const course = await Course.getCoursesStats(user_id, course_id);
  res.json(course);
}
module.exports = {
  getCourses,
  getCourseDetail,
  createCourses,
  updateCourses,
  deleteCourses,
  publishCourses,
  searchCourses,
  getCoursesStats
}